package com.min.star.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.min.star.model.Sexamination;
import com.min.star.service.ISexaminationService;
import com.min.star.ui.Json;

@Controller
@RequestMapping("/sexamination")
public class SexaminationController {

	@Autowired
	private ISexaminationService sexaminationService;

	/*
	 * 跳转到 查询页面
	 */
	@RequestMapping("/query")
	public String query(Model model) {

		model.addAttribute("sexamination", new Sexamination());
		return "/student/sexaminations";
	}

	@RequestMapping("/list")
	@ResponseBody // 将方法的返回值序列化json格式并返回前台
	public Object list(Sexamination sexamination) {
		// 通过Page<sexamination>对象设置查询条件,指定分页的页码和页的大小
		Page<Sexamination> page = new Page<>(sexamination.getPageNumber(), sexamination.getPageSize());
		// 通过page条件查询出来的记录存放到另一个Page对象 Page<sexamination> pager =
		Page<Sexamination> pager = this.sexaminationService.findPage(page, sexamination);
		Map<String, Object> map = new HashMap<>();
		map.put("rows", pager.getRecords());
		map.put("total", pager.getTotal());
		return map;
	}

	/**
	 * 根据id删除记录
	 * 
	 * @param publisherId
	 * @return
	 */
	@RequestMapping("/delete/{ids}")
	@ResponseBody
	public Object delete(@PathVariable("ids") String ids) {
		Json j = new Json();
		int ret = this.sexaminationService.delete(ids);
		if (ret > 0) {
			j.setMsg("删除成功！");
			j.setCode(0);
		} else {
			j.setMsg("删除失败！");
			j.setCode(-1);
		}
		return j;
	}

	@RequestMapping("/add")
	@ResponseBody
	public Object add(@Valid Sexamination sexamination, BindingResult result) {
		Json j = new Json();
		// 服务端验证
		if (result.hasErrors()) {
			return validMessage(result, j);
		}
		// save返回的是新增对象
		Sexamination info = this.sexaminationService.save(sexamination);
		if (info.getId() > 0) {
			j.setMsg("新增成功！");
			j.setObj(info);
			j.setCode(0);
		} else {
			j.setMsg("新增失败！");
			j.setCode(-1);
		}
		return j;
	}

	// 新增服务端验证打包
	private Object validMessage(BindingResult result, Json j) {
		String msg = "";
		List<ObjectError> allErrors = result.getAllErrors();
		if (allErrors != null && allErrors.size() > 0) {
			for (int i = 0; i < allErrors.size(); i++) {
				ObjectError error = allErrors.get(i);
				if (i == 0) {
					msg += error.getDefaultMessage() + "<br/>";
				} else {
					msg += "<i class='mdi mdi-emoticon-sad'></i>" + error.getDefaultMessage() + "<br/>";
				}
			}
		}
		j.setMsg(msg);
		j.setCode(-1);
		j.setObj(allErrors);
		return j;
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Object edit(@Valid Sexamination sexamination, BindingResult result) {
		Json j = new Json();
		// 服务端验证
		if (result.hasErrors()) {
			return validMessage(result, j);
		}
		//
		// save返回的是新增对象
		Sexamination info = this.sexaminationService.update(sexamination);
		if (info.getId() > 0) {
			j.setMsg("编辑成功！");
			j.setObj(info);
			j.setCode(0);
		} else {
			j.setMsg("编辑失败！");
			j.setCode(-1);
		}
		return j;
	}

}
